export function objNotNull(obj) {
    if (obj != null && obj != undefined) {
        return true;
    }
    return false;
}
export function strNotNull(obj) {
    if (objNotNull(obj)) {
        obj = String(obj);
        if (obj != '' && obj.length > 0) {
            return true;
        }
    }
    return false;
}
export function arrNotNull(obj) {
    if (objNotNull(obj) && obj.length > 0) {
        return true;
    }
    return false;
}
export function getParamValFromUrl(paramName) {
    var reg = new RegExp("(^|&)" + paramName + "=([^&]*)(&|$)", "i");
    var r = decodeURI(window.location.search).substr(1).match(reg);
    var val = (r != null) ? unescape(r[2]) : '';
    return val;
}

export function getQuery(key,_this) {
    console.info(_this.$route)
    if(_this.$route.query && _this.$route.query[key]){
        return _this.$route.query[key]
    }else{
        return null;
    }
}
export function caseToShow(obj) {
    return resolveEnter(strNotNull(obj) ? String(obj) : '--');
}

export function replaceAll(str, source, target) {
    return str.replace(new RegExp(source, "gm"), target);
}

export function resolveEnter(str) {
    return replaceAll(str, "\n", "<br/>")
}

//文件大小格式化
export function formatFileSize(limit) {
    var size = "";
    if (limit < 0.1 * 1024) { //小于0.1KB，则转化成B
        size = limit.toFixed(2) + "B"
    } else if (limit < 0.1 * 1024 * 1024) { //小于0.1MB，则转化成KB
        size = (limit / 1024).toFixed(2) + "KB"
    } else if (limit < 0.1 * 1024 * 1024 * 1024) { //小于0.1GB，则转化成MB
        size = (limit / (1024 * 1024)).toFixed(2) + "MB"
    } else { //其他转化成GB
        size = (limit / (1024 * 1024 * 1024)).toFixed(2) + "GB"
    }

    var sizeStr = size + ""; //转成字符串
    var index = sizeStr.indexOf("."); //获取小数点处的索引
    var dou = sizeStr.substr(index + 1, 2); //获取小数点后两位的值
    if (dou === "00") { //判断后两位是否为00，如果是则删除00
        return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2)
    }
    return size;
}

export function endWith(origin, suffix) {
    origin = origin.toLowerCase();
    suffix = suffix.toLowerCase();
    let index = origin.length - suffix.length;
    return index > 0 && origin.lastIndexOf(suffix) === index;
}

//传入一个date对象然后调用该方法
export function DateTimeFormat(date) {
    var y = date.getFullYear() //年
    var m = date.getMonth() + 1 //月
    m = m < 10 ? '0' + m : m
    var d = date.getDate() //日
    d = d < 10 ? '0' + d : d
    var h = date.getHours() //时
    var minute = date.getMinutes() //分
    minute = minute < 10 ? '0' + minute : minute
    var Seconds = date.getSeconds() //秒
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + Seconds
}

// 获取年份select
export function getYearList() {    
    const year = String(new Date().getFullYear())
    const lastNum = year.slice(year.length-1,year.length)  
    let yearList = []              
    for(var i = lastNum; i >= 0; i--){
        yearList.push({label: `202${i}年`,code: parseInt(`202${i}`)})
    }
    return yearList
}
